﻿using System;
using System.Data;
using System.Text;
using System.Data.SqlClient;
using Catering_DBOpertion;//Please add references
namespace Catering_Operation
{
    /// <summary>
    /// 数据访问类:RawMaterial
    /// </summary>
    public partial class D_RawMaterial
    {
        public D_RawMaterial()
        { }
        #region  Method

        /// <summary>
        /// 得到最大ID
        /// </summary>
        public int GetMaxId()
        {
            return DbHelperSQL.GetMaxID("id", "RawMaterial");
        }

        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public bool Exists(int id)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) from RawMaterial");
            strSql.Append(" where id=@id ");
            SqlParameter[] parameters = {
					new SqlParameter("@id", SqlDbType.Int,4)};
            parameters[0].Value = id;

            return DbHelperSQL.Exists(strSql.ToString(), parameters);
        }


        /// <summary>
        /// 增加一条数据
        /// </summary>
        public void Add(Catering_Model.E_RawMaterial model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into RawMaterial(");
            strSql.Append("id,RAWName,RAWTID,RAWID,RAWIDS,RAWSid,RAWWid,InDBid,Back,temp1,temp2,RAWZS,SafetyTips,UsefulPart)");
            strSql.Append(" values (");
            strSql.Append("@id,@RAWName,@RAWTID,@RAWID,@RAWIDS,@RAWSid,@RAWWid,@InDBid,@Back,@temp1,@temp2,@RAWZS,@SafetyTips,@UsefulPart)");
            SqlParameter[] parameters = {
					new SqlParameter("@id", SqlDbType.Int,4),
					new SqlParameter("@RAWName", SqlDbType.VarChar,50),
					new SqlParameter("@RAWTID", SqlDbType.Int,4),
					new SqlParameter("@RAWID", SqlDbType.Int,4),
					new SqlParameter("@RAWIDS", SqlDbType.Int,4),
					new SqlParameter("@RAWSid", SqlDbType.VarChar,50),
					new SqlParameter("@RAWWid", SqlDbType.VarChar,50),
					new SqlParameter("@InDBid", SqlDbType.VarChar,50),
					new SqlParameter("@Back", SqlDbType.Text),
					new SqlParameter("@temp1", SqlDbType.VarChar,200),
					new SqlParameter("@temp2", SqlDbType.VarChar,200),
                    new SqlParameter("@RAWZS", SqlDbType.Int,4),
                    new SqlParameter("@SafetyTips", SqlDbType.Text),
                    new SqlParameter("@UsefulPart", SqlDbType.Decimal,18)};
            parameters[0].Value = model.id;
            parameters[1].Value = model.RAWName;
            parameters[2].Value = model.RAWTID;
            parameters[3].Value = model.RAWID;
            parameters[4].Value = model.RAWIDS;
            parameters[5].Value = model.RAWSid;
            parameters[6].Value = model.RAWWid;
            parameters[7].Value = model.InDBid;
            parameters[8].Value = model.Back;
            parameters[9].Value = model.temp1;
            parameters[10].Value = model.temp2;
            parameters[11].Value = model.RAWZS;
            parameters[12].Value = model.SafetyTips;
            parameters[13].Value = model.UsefulPart;

            DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
        }
        /// <summary>
        /// 更新一条数据
        /// </summary>
        public bool Update(Catering_Model.E_RawMaterial model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update RawMaterial set ");
            strSql.Append("RAWName=@RAWName,");
            strSql.Append("RAWTID=@RAWTID,");
            strSql.Append("RAWID=@RAWID,");
            strSql.Append("RAWIDS=@RAWIDS,");
            strSql.Append("RAWSid=@RAWSid,");
            strSql.Append("RAWWid=@RAWWid,");
            strSql.Append("InDBid=@InDBid,");
            strSql.Append("Back=@Back,");
            strSql.Append("temp1=@temp1,");
            strSql.Append("temp2=@temp2,");
            strSql.Append("RAWZS=@RAWZS,");
            strSql.Append("SafetyTips=@SafetyTips,");
            strSql.Append("UsefulPart=@UsefulPart");
            strSql.Append(" where id=@id ");
            SqlParameter[] parameters = {
					new SqlParameter("@RAWName", SqlDbType.VarChar,50),
					new SqlParameter("@RAWTID", SqlDbType.Int,4),
					new SqlParameter("@RAWID", SqlDbType.Int,4),
					new SqlParameter("@RAWIDS", SqlDbType.Int,4),
					new SqlParameter("@RAWSid", SqlDbType.VarChar,50),
					new SqlParameter("@RAWWid", SqlDbType.VarChar,50),
					new SqlParameter("@InDBid", SqlDbType.VarChar,50),
					new SqlParameter("@Back", SqlDbType.Text),
					new SqlParameter("@temp1", SqlDbType.VarChar,200),
					new SqlParameter("@temp2", SqlDbType.VarChar,200),
                    new SqlParameter("@RAWZS", SqlDbType.Int,4),
                    new SqlParameter("@SafetyTips", SqlDbType.Text), 
                    new SqlParameter("@UsefulPart", SqlDbType.Decimal,18),  
					new SqlParameter("@id", SqlDbType.Int,4)};
            parameters[0].Value = model.RAWName;
            parameters[1].Value = model.RAWTID;
            parameters[2].Value = model.RAWID;
            parameters[3].Value = model.RAWIDS;
            parameters[4].Value = model.RAWSid;
            parameters[5].Value = model.RAWWid;
            parameters[6].Value = model.InDBid;
            parameters[7].Value = model.Back;
            parameters[8].Value = model.temp1;
            parameters[9].Value = model.temp2;
            parameters[10].Value = model.RAWZS;
            parameters[11].Value = model.SafetyTips;
            parameters[12].Value = model.UsefulPart;
            parameters[13].Value = model.id;

            int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool Delete(int id)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from RawMaterial ");
            strSql.Append(" where id=@id ");
            SqlParameter[] parameters = {
					new SqlParameter("@id", SqlDbType.Int,4)};
            parameters[0].Value = id;

            int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        /// <summary>
        /// 批量删除数据
        /// </summary>
        public bool DeleteList(string idlist)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from RawMaterial ");
            strSql.Append(" where id in (" + idlist + ")  ");
            int rows = DbHelperSQL.ExecuteSql(strSql.ToString());
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }


        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public Catering_Model.E_RawMaterial GetModel(int id)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("select  top 1 id,RAWName,RAWTID,RAWID,RAWIDS,RAWSid,RAWWid,InDBid,Back,temp1,temp2, RAWZS,SafetyTips,UsefulPart from RawMaterial ");
            strSql.Append(" where id=@id ");
            SqlParameter[] parameters = {
					new SqlParameter("@id", SqlDbType.Int,4)};
            parameters[0].Value = id;

            Catering_Model.E_RawMaterial model = new Catering_Model.E_RawMaterial();
            DataSet ds = DbHelperSQL.Query(strSql.ToString(), parameters);
            if (ds.Tables[0].Rows.Count > 0)
            {
                if (ds.Tables[0].Rows[0]["id"] != null && ds.Tables[0].Rows[0]["id"].ToString() != "")
                {
                    model.id = int.Parse(ds.Tables[0].Rows[0]["id"].ToString());
                }
                if (ds.Tables[0].Rows[0]["RAWName"] != null && ds.Tables[0].Rows[0]["RAWName"].ToString() != "")
                {
                    model.RAWName = ds.Tables[0].Rows[0]["RAWName"].ToString();
                }
                if (ds.Tables[0].Rows[0]["RAWTID"] != null && ds.Tables[0].Rows[0]["RAWTID"].ToString() != "")
                {
                    model.RAWTID = int.Parse(ds.Tables[0].Rows[0]["RAWTID"].ToString());
                }
                if (ds.Tables[0].Rows[0]["RAWID"] != null && ds.Tables[0].Rows[0]["RAWID"].ToString() != "")
                {
                    model.RAWID = int.Parse(ds.Tables[0].Rows[0]["RAWID"].ToString());
                }
                if (ds.Tables[0].Rows[0]["RAWIDS"] != null && ds.Tables[0].Rows[0]["RAWIDS"].ToString() != "")
                {
                    model.RAWIDS = int.Parse(ds.Tables[0].Rows[0]["RAWIDS"].ToString());
                }
                if (ds.Tables[0].Rows[0]["RAWSid"] != null && ds.Tables[0].Rows[0]["RAWSid"].ToString() != "")
                {
                    model.RAWSid = ds.Tables[0].Rows[0]["RAWSid"].ToString();
                }
                if (ds.Tables[0].Rows[0]["RAWWid"] != null && ds.Tables[0].Rows[0]["RAWWid"].ToString() != "")
                {
                    model.RAWWid = ds.Tables[0].Rows[0]["RAWWid"].ToString();
                }
                if (ds.Tables[0].Rows[0]["InDBid"] != null && ds.Tables[0].Rows[0]["InDBid"].ToString() != "")
                {
                    model.InDBid = ds.Tables[0].Rows[0]["InDBid"].ToString();
                }
                if (ds.Tables[0].Rows[0]["Back"] != null && ds.Tables[0].Rows[0]["Back"].ToString() != "")
                {
                    model.Back = ds.Tables[0].Rows[0]["Back"].ToString();
                }
                if (ds.Tables[0].Rows[0]["temp1"] != null && ds.Tables[0].Rows[0]["temp1"].ToString() != "")
                {
                    model.temp1 = ds.Tables[0].Rows[0]["temp1"].ToString();
                }
                if (ds.Tables[0].Rows[0]["temp2"] != null && ds.Tables[0].Rows[0]["temp2"].ToString() != "")
                {
                    model.temp2 = ds.Tables[0].Rows[0]["temp2"].ToString();
                }
                if (ds.Tables[0].Rows[0]["RAWZS"] != null && ds.Tables[0].Rows[0]["RAWZS"].ToString() != "")
                {
                    model.RAWZS = int.Parse(ds.Tables[0].Rows[0]["RAWZS"].ToString());
                }
                if (ds.Tables[0].Rows[0]["SafetyTips"] != null && ds.Tables[0].Rows[0]["SafetyTips"].ToString() != "")
                {
                    model.SafetyTips = ds.Tables[0].Rows[0]["SafetyTips"].ToString();
                }
                if (ds.Tables[0].Rows[0]["UsefulPart"] != null && ds.Tables[0].Rows[0]["UsefulPart"].ToString() != "")
                {
                    model.UsefulPart = decimal.Parse(ds.Tables[0].Rows[0]["UsefulPart"].ToString());
                }
                return model;
            }
            else
            {
                return null;
            }
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetList(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select id,RAWName,RAWTID,RAWID,RAWIDS,RAWSid,RAWWid,InDBid,Back,temp1,temp2 ,RAWZS,SafetyTips,UsefulPart");
            strSql.Append(" FROM RawMaterial ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }

            strSql.Append(" order by RAWName ");
            return DbHelperSQL.Query(strSql.ToString());
        }

        /// <summary>
        /// 获得前几行数据
        /// </summary>
        public DataSet GetList(int Top, string strWhere, string filedOrder)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select ");
            if (Top > 0)
            {
                strSql.Append(" top " + Top.ToString());
            }
            strSql.Append(" id,RAWName,RAWTID,RAWID,RAWIDS,RAWSid,RAWWid,InDBid,Back,temp1,temp2,RAWZS,SafetyTips,UsefulPart ");
            strSql.Append(" FROM RawMaterial ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            strSql.Append(" order by " + filedOrder);
            return DbHelperSQL.Query(strSql.ToString());
        }

        
        #endregion  Method

        #region 扩展方法
        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetList(string strWhere, string field)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select " + field);
            strSql.Append(" FROM RawMaterial ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }

            strSql.Append(" order by RAWName ");
            return DbHelperSQL.Query(strSql.ToString());
        }

        /// <summary>
        /// 通过区域获取符合要求的原料数据列表
        /// </summary>
        /// <param name="strWhere"></param>
        /// <param name="field"></param>
        /// <returns></returns>
        public DataSet GetListByAreaID(string strWhere, string field)
        {
            //select * from RawMaterial as A right join RawMaterial_Area as B on A.id=B.RawID where B.AreaID=1
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select " + field);
            strSql.Append(" FROM RawMaterial as A right join RawMaterial_Area as B on A.id=B.RawID ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }

            strSql.Append(" order by RAWName ");
            return DbHelperSQL.Query(strSql.ToString());
        }

        public DataTable GetRawMaterialById(int id, int Type)
        {
            StringBuilder sql = new StringBuilder();
            sql.Append(@"Declare @sql varchar(8000)
Set @sql = 'Select RAWName as 食物名称'
Select @sql = @sql + ',sum(case NutrientName when '''+NutrientName+''' then CONVERT(decimal(18,2),Content) else 0 end) ['+NutrientName+']'
from (select distinct NutrientName from Shiwuyingyang) as Shiwuyingyang
Select @sql = @sql+' from Shiwuyingyang ");
            switch (Type)
            {
                case 0:
                    sql.Append(@" where RawMid = ");
                    break;
                case 1:
                    sql.Append(@" where id =  ");
                    break;
                default:
                    break;
            }

            sql.Append(" " + id);
            sql.Append(" and isnumeric(content) = 1");
            sql.Append(@" group by RAWName ' Exec (@sql)");
            return DbHelperSQL.Query(sql.ToString()).Tables[0];
        }

        public DataTable GetRawMaterialByIdtoProc(int id, int Type)
        {
            StringBuilder sql = new StringBuilder();
            switch (Type)
            {
                case 0:
                    sql.Append(@" where RawMid = " + id);
                    break;
                case 1:
                    sql.Append(@" where id =  " + id);
                    break;
                default:
                    break;
            }
            SqlParameter[] parm = new SqlParameter[]{
                new SqlParameter("@strwhere",sql.ToString())
            };
            return DbHelperSQL.RunProcedureReDataTable("Proc_ShiWuYingYang", parm);
        }

        public int UpdateShiwuYingyangByIdAndNutrientName(string id, string nutrientName, string content)
        {
            int num = 0;
            string checksql = "select * from [CMS_Catering].[dbo].[ShiwuYingyang] WHERE id=@i and nutrientName=@n";
            SqlParameter[] parm = new SqlParameter[] { 
                new SqlParameter("@c",content),
                new SqlParameter("@i",id),
                new SqlParameter("@n",nutrientName)
            };
            DataTable dt = DbHelperSQL.Query(checksql, parm).Tables[0];
            string sql = "";
            if (dt.Rows.Count != 0)
            {
                sql = @"UPDATE [CMS_Catering].[dbo].[ShiwuYingyang]
   SET [Content] = @c
 WHERE id=@i and nutrientName=@n";
            }
            else
            {
                string nTypeid = DbHelperSQL.Query("SELECT [id] FROM [CMS_Catering].[dbo].[NutrientType] where [NutrientName]='" + nutrientName + "'").Tables[0].Rows[0][0].ToString();
                parm = new SqlParameter[] { 
                new SqlParameter("@r",id),
                new SqlParameter("@n",nTypeid),
                new SqlParameter("@c",content)
            };
                sql = @"INSERT INTO [CMS_Catering].[dbo].[NutrientComposition]
           ([RawID]
           ,[NutritionElementsID]
           ,[Content])
     VALUES
           (@r,@n,@c)";
            }
            num = DbHelperSQL.ExecuteSql(sql, parm);
            return num;
        }

        #endregion
    }
}

